package com.yiwei.zhh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.constant.UserGlobal;
import com.yiwei.zhh.event.OnCloseMyfund;
import com.yiwei.zhh.event.OnHomeEventMsg;
import com.yiwei.zhh.http.InfoCallBack;
import com.yiwei.zhh.impl.RepayInfoImpl;
import com.yiwei.zhh.model.RepayModel;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.PreferenceUtils;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.Util;
import com.yiwei.zhh.view.Dialog;
import com.yiwei.zhh.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3MySettingActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_phone_left)
    TextView txtPhoneLeft;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.img_phone_icon)
    ImageView imgPhoneIcon;
    @BindView(R.id.layout_phone)
    RelativeLayout layoutPhone;
    @BindView(R.id.txt_password_left)
    TextView txtPasswordLeft;
    @BindView(R.id.txt_password)
    TextView txtPassword;
    @BindView(R.id.img_password_icon)
    ImageView imgPasswordIcon;
    @BindView(R.id.layout_password)
    RelativeLayout layoutPassword;
    @BindView(R.id.txt_gesture_left)
    TextView txtGestureLeft;
    @BindView(R.id.txt_gesture)
    TextView txtGesture;
    @BindView(R.id.img_gesture_icon)
    ImageView imgGestureIcon;
    @BindView(R.id.layout_gesture)
    RelativeLayout layoutGesture;
    @BindView(R.id.txt_help_left)
    TextView txtHelpLeft;
    @BindView(R.id.layout_help)
    RelativeLayout layoutHelp;
    @BindView(R.id.layout_about)
    RelativeLayout layoutAbout;
    @BindView(R.id.txt_version_left)
    TextView txtVersionLeft;
    @BindView(R.id.txt_version)
    TextView txtVersion;
    @BindView(R.id.img_version_icon)
    ImageView imgVersionIcon;
    @BindView(R.id.layout_version)
    RelativeLayout layoutVersion;
    @BindView(R.id.btn)
    TextView btn;
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("设置");
    }

    private void initButton() {
        if (!UserGlobal.checkLogin()) {

            btn.setText(getResources().getString(R.string.login));

            txtPhone.setText(PreferenceUtils.getString(this, "mobile", ""));

        } else {
            btn.setText(getResources().getString(R.string.logout));
            txtPhone.setText("");
        }
        txtVersion.setText(Util.getVersion(this));
        if (UserGlobal.getIsOpenGuesture()) {
            txtGesture.setText("已开启");
        } else {
            txtGesture.setText("未开启");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initButton();
    }

    /**
     * 是否登录
     *
     * @return
     */
    private boolean isLogin() {
        if (!PreferenceUtils.getBoolean(this, "is_logined", false)) {
            ToastUtil.makeText(this, getResources().getString(R.string.please_login), Toast.LENGTH_SHORT);
            Util.startActivity(this, V3LoginByCodeActivity.class);
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.layout_phone, R.id.layout_password, R.id.layout_gesture, R.id.layout_help, R.id.layout_about, R.id.layout_version, R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.layout_phone:
                if (isLogin()) getRepayInfo();
                break;
            case R.id.layout_password:
                if (isLogin()) {
//                    if (UserGlobal.isSetPass()) {
//                        Util.startActivity(this, UpdatePwdActivity.class);
//                    } else {
//                        Util.startActivity(this, V3SetNewPassActivity.class);
//                    }
                    Util.startActivity(this, V3ForgetPassActivity.class);
                }
                break;
            case R.id.layout_gesture:
                if (isLogin())
                Util.startActivity(this, GestureSettingActivity.class);
                break;
            case R.id.layout_help:
                Intent helpIntent = new Intent(this, HelpCenterActivity.class);
                helpIntent.putExtra("type", 1);
                startActivity(helpIntent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case R.id.layout_about:
                Util.startActivity(this, ContactUsActivity.class);
                break;
            case R.id.layout_version:
                Util.startActivity(this, VersionControlActivity.class);
                break;
            case R.id.btn:
                if (UserGlobal.checkLogin()) {
                    Dialog.showDefaultV3Dialog(this, "", "确定退出登录吗？", "取消", "确定", new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {
                            logout();
                        }

                        @Override
                        public void cancel() {

                        }
                    });


                } else {
                    Util.startActivity(this, V3LoginByCodeActivity.class);
                    finish();
                }
                break;
        }
    }

    public void logout() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        Api.api_service.getLogoutInfo().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(getApplicationContext(), result.getMsg(), Toast.LENGTH_SHORT).show();
//        Util.clearUserInfo(getApplicationContext());
                    UserGlobal.logout();
                    UserGlobal.setIsOpenGuesture(false);
                    clearAcache();
                    Map<String, String> map = new HashMap<>();
                    map.put("type", Const.SendType.SWITCH_PAGE);
                    map.put("currentIndex", 0 + "");
                    EventBus.getDefault().post(new OnHomeEventMsg(map));
                    Util.startActivity(this, MainActivityNew.class);
//                    finish();
//                    initButton();
//        sendBroadcast();
                    finish();
                });
    }

    /**
     * 获取还款信息
     */
    private void getRepayInfo() {
        new RepayInfoImpl().getRepayInfo(this, new InfoCallBack() {
            @Override
            public void onSuccess(Object o) {
                RepayModel model = (RepayModel) o;
                if (TextUtils.isEmpty(model.getData().getId())) {
//                    0未实名，1已实名
                    if (PreferenceUtils.getString(V3MySettingActivity.this, "idcardpassed", "0").equals("0")) {
                        Dialog.gotoShiMing(V3MySettingActivity.this, new Dialog.DialogDefaultClickListener() {
                            @Override
                            public void confirm() {
                                Util.startActivity(V3MySettingActivity.this, V3IdcardIdentityActivity.class);
                            }

                            @Override
                            public void cancel() {

                            }
                        }).show();
                    } else {
                        Util.startActivity(V3MySettingActivity.this, ChangePhoneTotalActivity.class);
                    }
                    return;
                }
//                有借款
                Dialog.showDefaultV3Dialog(V3MySettingActivity.this, "提示", "您有账单未还清\n请还款后再进行提现", "取消", "去还款", new Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        EventBus.getDefault().post(new OnCloseMyfund());
//                        sendEventBus();
                        finish();
                    }

                    @Override
                    public void cancel() {

                    }
                });

//                Dialog.gotoRepayMoney(V3MySettingActivity.this, new Dialog.DialogDefaultClickListener() {
//                    @Override
//                    public void confirm() {
//                        EventBus.getDefault().post(new OnCloseMyfund());
////                        sendEventBus();
//                        finish();
//                    }
//
//                    @Override
//                    public void cancel() {
//
//                    }
//                }).show();
            }

            @Override
            public void onFailure(String cause) {
                ToastUtil.makeText(V3MySettingActivity.this, cause, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
